/*
* name: on-scroll
* version: v0.2.2
* update: 修复滚动解锁bug；调整onshow时机；
* date: 2014-12-11
*/

define(function(require, exports, module) {
	var $ = require('jquery');
	var base = require('base');
	$.fn.onScroll = function(config) {
		var $this = $(this),
			$window = $(window),
			winH = $window.height(),
			opt = {
				offset:0,
				target:null,
				speed:256,
				adsorb:false,
				onshow:function(){}
			},
			$target,
			watchScroll,
			scrollCatch = $window.scrollTop(),
			scrollDown,
			scrollLock,
			_setTime;
		if(!$this.length) return;
		$.extend(opt, config || {});
		// 获取目标及绑定事件
		if( opt.target && $(opt.target).children('li').length ){
			$target = $(opt.target);
			if($this.length!==$target.children('li').length){
				if(window.console){
					console.log("'."+$this.selector+"' 与滚动绑定的目标数量不同，无法执行绑定");
				};
				return false;
			}
			var targetTop = $target.offset().top;
			$target.on('click','li',function(e){
				e.preventDefault();
				var _posi = $this.eq($(this).index()).offset().top - opt.offset;
				scrollLock = true;
				$('html,body').stop(1).animate({"scrollTop":_posi},
					opt.speed,
					function(){
						clearTimeout(_setTime);
						_setTime = setTimeout(function(){
							scrollLock = false;
						},160)
					}
				);
			})
		}
		watchScroll = function(winH, winT){
			// 添加目标定位类
			if( $target ){
				if(targetTop <= winT + opt.offset){
					$target.addClass('fixed');
				}else{
					$target.removeClass('fixed');
				}
			}

			// 滚动定位（函数节流）
			base.throttle(function(){
				var _eIndex;
				if(scrollCatch<winT){
					scrollDown = true;
				}else{
					scrollDown = false;
				}
				scrollCatch = winT;

				$this.each(function(i,e){	
					if( parseInt($(e).offset().top) <= winH + winT + opt.offset ){
						_eIndex = $(e).index($this.selector);
						if($(e).data('done')) return true;
						$(e).data('done',1);
						opt.onshow && opt.onshow(e);
					}
				});

				$target && $target.children('li').eq(_eIndex).addClass('active')
				.siblings().removeClass('active');
				// 吸附
				if(!opt.adsorb) return;
				if(!scrollLock && scrollDown===true && _eIndex>-1 && _eIndex<$this.length-1){
					scrollLock = true;
					_eIndex++;
					$('html,body').stop(1).animate({"scrollTop":$this.eq(_eIndex).offset().top - opt.offset},
						opt.speed,
						function(){
							clearTimeout(_setTime);
							_setTime = setTimeout(function(){
								scrollLock = false;
							},160)
						}
					);
				}else if(!scrollLock && scrollDown===false && _eIndex>=0){
					scrollLock = true;
					$('html,body').stop(1).animate({"scrollTop":$this.eq(_eIndex).offset().top - opt.offset},
						opt.speed,
						function(){
							clearTimeout(_setTime);
							_setTime = setTimeout(function(){
								scrollLock = false;
							},160)
						}
					);
				}
			},80)
			
		}
		watchScroll(winH, $window.scrollTop());

		$window.bind({
			'scroll':function (){
				watchScroll(winH, $window.scrollTop() );
			},
			'resize':function(){
				watchScroll($window.height(), $window.scrollTop() );
			}
		});

		return $this;
	}
})